#include <memory>
#include <iostream>
#include <utility>

struct Point {
    double x, y, z;

    Point(double px, double py, double pz) : x(px), y(py), z(pz) {}
    ~Point() { std::cout << "Point destructed\n"; }
};

int main() {
    std::allocator<Point> allocator; // 创建 Point 类型的分配器

    // 分配内存但不构造对象
    Point* points = allocator.allocate(3); // 分配足够内存以存储3个Point对象

#if __cplusplus >= 202002L
    // 使用std::construct_at 在分配的内存中创建对象
    std::construct_at(points, 1.0, 2.0, 3.0);
    std::construct_at(points + 1, 4.0, 5.0, 6.0);
    std::construct_at(points + 2, 7.0, 8.0, 9.0);
#else
    // 使用allocator.construct 在分配的内存中创建对象
    allocator.construct(points, 1.0, 2.0, 3.0);
    allocator.construct(points + 1, 4.0, 5.0, 6.0);
    allocator.construct(points + 2, 7.0, 8.0, 9.0);
#endif

    // 遍历并打印点的坐标
    for (int i = 0; i < 3; i++) {
        std::cout << "Point: (" << points[i].x << ", " << points[i].y << ", " << points[i].z << ")" << std::endl;
    }

#if __cplusplus >= 202002L
    // 使用std::destroy_at 析构对象
    for (int i = 0; i < 3; i++) {
        std::destroy_at(points + i);
    }
#else
    // 使用allocator.destroy 析构对象
    for (int i = 0; i < 3; i++) {
        allocator.destroy(points + i);
    }
#endif
    allocator.deallocate(points, 3); // 释放内存

    return 0;
}